<?php

class PotreroController extends BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$potreros = Potrero::orderBy('id','DESC')->get();
		return View::make('potreros.index')->with('potreros',$potreros);
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('potreros.create');
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$potrero = new Potrero;
		$potrero->cod_potrero = Input::get('cod_potrero');
		$potrero->descripcion = Input::get('descripcion');
		$potrero->activo = Input::get('activo');
		if($potrero->save()){
			Session::flash('message','Guardado Correctamente');
			Session::flash('class','success');
		}else{
			Session::flash('message','Ha ocurrido un error');
			Session::flash('class','danger');
		}
		return Redirect::to('/potreros');
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id = null)
	{
		$potrero = Potrero::find($id);
		return View::make('potreros.show')->with('potrero',$potrero);
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id = null)
	{
		$potrero = Potrero::find($id);
		return View::make('potreros.edit')->with('potrero',$potrero);
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$potrero = Potrero::find($id);
		$potrero->cod_potrero = Input::get('cod_potrero');
		$potrero->descripcion = Input::get('descripcion');
		$potrero->activo = Input::get('activo');
		if($potrero->save()){
			Session::flash('message','Actualizado Correctamente');
			Session::flash('class','success');
		}else{
			Session::flash('message','Ha ocurrido un error');
			Session::flash('class','danger');
		}
		return Redirect::to('potreros');
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$potrero = Potrero::find($id);
		if($potrero->delete()){
			Session::flash('message','Eliminado Correctamente');
			Session::flash('class','success');
		}else{
			Session::flash('message','Ha ocurrido un error');
			Session::flash('class','danger');
		}
		return Redirect::to('potreros');
	}


}
